package cn.iocoder.yudao.module.blog.controller.admin.space;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.blog.controller.admin.space.vo.*;
import cn.iocoder.yudao.module.blog.dal.dataobject.space.BlogSpaceDO;
import cn.iocoder.yudao.module.blog.convert.space.BlogSpaceConvert;
import cn.iocoder.yudao.module.blog.service.space.BlogSpaceService;

@Tag(name = "管理后台 - 动态")
@RestController
@RequestMapping("/blog/space")
@Validated
public class BlogSpaceController {

    @Resource
    private BlogSpaceService spaceService;

    @PostMapping("/create")
    @Operation(summary = "创建动态")
    @PreAuthorize("@ss.hasPermission('blog:space:create')")
    public CommonResult<Long> createSpace(@Valid @RequestBody BlogSpaceCreateReqVO createReqVO) {
        return success(spaceService.createSpace(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新动态")
    @PreAuthorize("@ss.hasPermission('blog:space:update')")
    public CommonResult<Boolean> updateSpace(@Valid @RequestBody BlogSpaceUpdateReqVO updateReqVO) {
        spaceService.updateSpace(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除动态")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blog:space:delete')")
    public CommonResult<Boolean> deleteSpace(@RequestParam("id") Long id) {
        spaceService.deleteSpace(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得动态")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blog:space:query')")
    public CommonResult<BlogSpaceRespVO> getSpace(@RequestParam("id") Long id) {
        BlogSpaceDO space = spaceService.getSpace(id);
        return success(BlogSpaceConvert.INSTANCE.convert(space));
    }

    @GetMapping("/list")
    @Operation(summary = "获得动态列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('blog:space:query')")
    public CommonResult<List<BlogSpaceRespVO>> getSpaceList(@RequestParam("ids") Collection<Long> ids) {
        List<BlogSpaceDO> list = spaceService.getSpaceList(ids);
        return success(BlogSpaceConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得动态分页")
    @PreAuthorize("@ss.hasPermission('blog:space:query')")
    public CommonResult<PageResult<BlogSpaceRespVO>> getSpacePage(@Valid BlogSpacePageReqVO pageVO) {
        PageResult<BlogSpaceDO> pageResult = spaceService.getSpacePage(pageVO);
        return success(BlogSpaceConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出动态 Excel")
    @PreAuthorize("@ss.hasPermission('blog:space:export')")
    @OperateLog(type = EXPORT)
    public void exportSpaceExcel(@Valid BlogSpaceExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BlogSpaceDO> list = spaceService.getSpaceList(exportReqVO);
        // 导出 Excel
        List<BlogSpaceExcelVO> datas = BlogSpaceConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "动态.xls", "数据", BlogSpaceExcelVO.class, datas);
    }

}
